﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MedReg
{
    public partial class EducationInstitution : IEquatable<EducationInstitution>
    {
        public void ImportList(List<Classes.EducationInstitution> edInstList)
        {
            var mdb = new MedregDBDataContext();
            var ListEdInst = new List<EducationInstitution>();
            foreach (var edInst in edInstList)
            {
                EducationInstitution edInst1 = new EducationInstitution() { ID = edInst.ID, Name = edInst.Name, Parent = edInst.Parent};
                EducationInstitution query;

                if (mdb.EducationInstitution.LongCount() == 0)
                {
                    mdb.EducationInstitution.InsertOnSubmit(edInst1);
                }
                else
                {
                    try
                    {
                        query = mdb.EducationInstitution.Single(l => l.ID == edInst1.ID);
                        query.Name = edInst1.Name;

                    }
                    catch (ArgumentNullException e)
                    {
                    }
                    catch (InvalidOperationException e)
                    {

                        if (!ListEdInst.Contains(edInst1))
                        {
                            mdb.EducationInstitution.InsertOnSubmit(edInst1);
                            ListEdInst.Add(edInst1);
                        }
                    }
                }
            }
            mdb.SubmitChanges();
        }
        public bool Equals(EducationInstitution other)
        {
            var i = this.ID.Equals(other.ID);
            var j = this.Name.Equals(other.Name);
            if (i & j)
            {
                return true;
            }
            return false;
        }
    }
}
